package com.cbgolf.oa.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import com.cbgolf.oa.entity.DateRange;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author lq
 */
public class DateUtil {


  public static String formatWithbias = "yyyy/MM/dd HH:mm:ss";
  public static String formatYMDWithPoint = "yyyy.MM.dd";
  public static String formatYMDHm = "yyyy-MM-dd HH:mm";
  public static String formatYMD = "yyyy-MM-dd";
  public static String formatYMDBias = "yyyy/MM/dd";
  public static String formatMD_Zh = "MM月dd日";
  public static String formatM_Zh = "MM月";
  public static String formatMD = "MM-dd";
  public static String formatMDBias = "MM/dd";
  public static String formatD = "dd";
  public static String formatYM_Zh = "yyyy年M月";
  public static String formatYMD_Zh = "yyyy年M月dd日";
  public static String formatYMDhm_Zh = "yyyy年M月dd日 HH:mm";
  public static String formatHm = "HH:mm";
  public static String formatMdHm = "MM-dd HH:mm";

  public static final int DAYS_OF_WEEK = 7;


  /**
   * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
   * @author fy.zhang
   */
  public static String millToDate(long mss) {
    String time = "";
    if (mss <= 0) {
      return time;
    }
    long years = mss / (1000 * 60 * 60 * 24 * 365);
    long days = (mss / (1000 * 60 * 60 * 24)) % 356;
    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    long seconds = (mss % (1000 * 60)) / 1000;
    if (years > 0) {
      time += years + "年";
    }
    if (days > 0) {
      time += days + "天";
    }
    if (hours > 0) {
      time += hours + "小时";
    }
    if (minutes > 0) {
      time += minutes + "分";
    }
    if (seconds > 0) {
      time += seconds + "秒";
    }
    return time;
  }

  /**
   * @param begin 时间段的开始
   * @param end 时间段的结束
   * @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
   * @author fy.zhang
   */
  public static String formatDuring(Date begin, Date end) {
    return millToDate(end.getTime() - begin.getTime());
  }

  public static String getMonth(String time) {
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
      long lt = new Long(time);
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
      if (TextUtil.startWith(res, "0")) {
        res = TextUtil.replaceAll(res, "0");
      }

    } catch (Exception e) {

    }
    return res;
  }

  @SuppressLint("SimpleDateFormat")
  public static int compare_date(String DATE1, String DATE2) {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    try {
      Date dt1 = df.parse(DATE1);
      Date dt2 = df.parse(DATE2);
      if (dt1.getTime() > dt2.getTime()) {
        return 1;
      } else if (dt1.getTime() < dt2.getTime()) {
        return -1;
      } else {
        return 0;
      }
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    return 0;
  }

  /*
   * 将时间转换为时间戳
   */
  public static String dateToStamp(String s) {
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = null;
      try {
        date = simpleDateFormat.parse(s);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      long ts = date.getTime();
      res = String.valueOf(ts);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间转换为时间戳
   */
  public static String dateToStamp(String s, String format) {
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
      Date date = null;
      try {
        date = simpleDateFormat.parse(s);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      long ts = date.getTime();
      res = String.valueOf(ts);
    } catch (Exception e) {

    }
    return res;
  }


  /*
   * 将时间转换为时间戳
   */
  public static String dateToStampYMD(String s) {
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
      Date date = null;
      try {
        date = simpleDateFormat.parse(s);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      long ts = date.getTime();
      res = String.valueOf(ts);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间转换为时间戳
   */
  public static String dateToStampyMdHm(String s) {
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      Date date = null;
      try {
        date = simpleDateFormat.parse(s);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      long ts = date.getTime();
      res = String.valueOf(ts);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间转换为时间戳
   */
  public static long dateToStampyMdHm2Long(String s) {
    long res = -1;
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      Date date = null;
      try {
        date = simpleDateFormat.parse(s);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      res = date.getTime();
//        res = String.valueOf(ts);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDate(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDate(Object stamp, String format) {
    if (Util.isNull(stamp) || Util.isNull(format) || TypeUtil.parseLong(stamp) <= 0) {
      return "";
    }
    String res = "";
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
      long lt = new Long(stamp.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateyMdHm(Object s) {

    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateyMdHm2(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateHm(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

//  /*
//   * 将时间戳转换为时间
//   */
//  public static String stampToDateHms(String s) {
//    String res = "";
//    if (TypeUtil.parseLong(s) <= 0) {
//      return res;
//    }
//    try {
//      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
//      long lt = new Long(s);
//      Date date = new Date(lt);
//      res = simpleDateFormat.format(date);
//    } catch (Exception e) {
//
//    }
//    return res;
//  }

//  /*
//   * 将时间戳转换为时间
//   */
//  public static String stampToDateyMd(String s) {
//    String res = "";
//    if (TypeUtil.parseLong(s) <= 0) {
//      return res;
//    }
//    try {
//      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//      long lt = new Long(s);
//      Date date = new Date(lt);
//      res = simpleDateFormat.format(date);
//    } catch (Exception e) {
//
//    }
//    return res;
//  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateyMd2(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateMd2(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
  };
  private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      return new SimpleDateFormat("yyyy-MM-dd");
    }
  };

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateyMd3(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDateMd(Object s) {
    String res = "";
    if (TypeUtil.parseLong(s) <= 0) {
      return res;
    }

    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
      long lt = new Long(s.toString());
      Date date = new Date(lt);
      res = simpleDateFormat.format(date);
    } catch (Exception e) {

    }
    return res;
  }

  public static String getDateTime(String sdate) {
    Date time = toDate(sdate);
    if (time == null) {
      return "";
    }
    String ftime = "";
    Calendar cal = Calendar.getInstance();

    // 判断是否是同一天
    String curDate = dateFormater2.get().format(cal.getTime());
    String paramDate = dateFormater2.get().format(time);
    if (curDate.equals(paramDate)) {
      int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
      if (hour == 0) {
        ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
      } else {
        ftime = hour + "小时前";
      }
      return ftime;
    }

    long lt = time.getTime() / 86400000;
    long ct = cal.getTimeInMillis() / 86400000;
    int days = (int) (ct - lt);
    if (days == 0) {
      int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
      if (hour == 0) {
        ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
      } else {
        ftime = hour + "小时前";
      }
    } else if (days == 1) {
      ftime = "昨天";
    } else if (days == 2) {
      ftime = "前天";
    } else if (days > 2 && days <= 10) {
      ftime = days + "天前";
    } else if (days > 10) {
      ftime = dateFormater2.get().format(time);
    }
    return ftime;
  }

  @SuppressLint("SimpleDateFormat")
  @SuppressWarnings("deprecation")
  public static String getWeek(long time, String unit) {
    if (time <= 0) {
      return "";
    }
    String week = TextUtil.Text(unit);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
    switch (c.get(Calendar.DAY_OF_WEEK)) {
      case 1:
        week += "日";
        break;
      case 2:
        week += "一";
        break;
      case 3:
        week += "二";
        break;
      case 4:
        week += "三";
        break;
      case 5:
        week += "四";
        break;
      case 6:
        week += "五";
        break;
      case 7:
        week += "六";
        break;
      default:
        break;
    }
    return week;
  }

  public static int getWeekIndex(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    return getWeekIndex(calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
  }

  /**
   * 1-6 代表星期一至星期六 0 代表星期日
   * 如果返回 -1 表示参数不正确
   */
  public static int getWeekIndex(int y, int m, int d) {
    if (m < 1 || m > 12 || d < 1 || d > 31) {
      return -1;
    }
    if (m == 1) {
      m = 13;
      y--;
    } else if (m == 2) {
      m = 14;
      y--;
    }
    int c = y / 100;
    y = y - c * 100;
    int week = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
    week %= 7;
    week = week < 0 ? week + 7 : week;
    return week;
  }

  public static Date toDate(String sdate) {
    sdate = sdate.replaceAll("/", "-");
    try {
      return dateFormater.get().parse(sdate);
    } catch (ParseException e) {
      return null;
    }
  }

  public static int getDayString(Long startTime) {
    int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
    long today = (System.currentTimeMillis() + offSet) / 86400000;
    long start = (startTime + offSet) / 86400000;
    return (int) (start - today);
  }


  //获取小时
  public static int getHour(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(calendar.HOUR_OF_DAY);
  }

  //获取分钟
  public static int getMinute(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(calendar.MINUTE);
  }

  //获取周
  public static int getWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
  }

  public static String[] weeks = new String[]{
      "日", "一", "二", "三", "四", "五", "六"
  };

  /**
   * 1-6 代表星期一至星期六 0 代表星期日
   * 如果返回 -1 表示参数不正确
   */
  public static int getWeek(int y, int m, int d) {
    if (m < 1 || m > 12 || d < 1 || d > 31) {
      return -1;
    }
    if (m == 1) {
      m = 13;
      y--;
    } else if (m == 2) {
      m = 14;
      y--;
    }
    int c = y / 100;
    y = y - c * 100;
    int week = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
    week %= 7;
    week = week < 0 ? week + 7 : week;
    return week;
  }

  //获取周
  public static String getWeek(long time) {
    if (time <= 0) {
      return "";
    }
    String res = "";
    try {
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(time);
      return weeks[getWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
          calendar.get(Calendar.DAY_OF_MONTH))];
    } catch (Exception e) {

    }
    return res;
  }

  //获取周
  public static String getWeek(String stampTime) {
    return getWeek(TypeUtil.parseLong(stampTime));
  }

  //获取周
//    public static int getWeek(int year, int moth, int day) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(year, moth - 1, day);
//        return calendar.get(Calendar.DAY_OF_WEEK);
//    }

  //获取年
  public static int getYear(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.YEAR);
  }

  public static int getYear(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    return calendar.get(Calendar.YEAR);
  }

  //获取月
  public static int getMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MONTH) + 1;
  }

  //获取月
  public static int getMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    return calendar.get(Calendar.MONTH) + 1;
  }

  //获取日
  public static int getDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DATE);
  }

  public static int getDay(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    return calendar.get(Calendar.DATE);
  }

  public static int getDay(String stampTime) {
    return getDay(TypeUtil.parseLong(stampTime));
  }

  public static Date getDate(int year, int moth, int day, int hour, int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, moth - 1, day, hour, minute);
    return calendar.getTime();
  }

  public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
  }


  public static void main(String[] args) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
    try {
      Date date = format.parse("2016-12-15 12");
      System.out.println(getHour(date));
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public static boolean isStamp(Object obj) {
    if (!Util.isNull(obj)) {
      try {
        String time = stampToDate(obj.toString());
        return true;
      } catch (Exception e) {
        return false;
      }
    }
    return false;
  }

  //获取当天的开始时间
  public static java.util.Date getDayBegin() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }

  //获取当天的结束时间
  public static java.util.Date getDayEnd() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    return cal.getTime();
  }

  //获取当天的开始时间
  public static long getDayBegin(int year, int month, int day) {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
  }

  //获取当天的结束时间
  public static long getDayEnd(int year, int month, int day) {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    return cal.getTimeInMillis();
  }

  //获取当天的开始时间
  public static long getDayBegin(long time) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
  }

  //获取当天的结束时间
  public static long getDayEnd(long time) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    return cal.getTimeInMillis();
  }

  //获取昨天的开始时间
  public static Date getBeginDayOfYesterday() {
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayBegin());
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
  }

  //获取昨天的结束时间
  public static Date getEndDayOfYesterDay() {
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayEnd());
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
  }

  //获取明天的开始时间
  public static Date getBeginDayOfTomorrow() {
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayBegin());
    cal.add(Calendar.DAY_OF_MONTH, 1);

    return cal.getTime();
  }

  //获取明天的结束时间
  public static Date getEndDayOfTomorrow() {
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayEnd());
    cal.add(Calendar.DAY_OF_MONTH, 1);
    return cal.getTime();
  }

  //获取本周的开始时间
  public static Date getBeginDayOfWeek() {
    Date date = new Date();
    if (date == null) {
      return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek == 1) {
      dayofweek += 7;
    }
    cal.add(Calendar.DATE, 2 - dayofweek);
    return getDayStartTime(cal.getTime());
  }

  //获取本周的结束时间
  public static Date getEndDayOfWeek() {
    Calendar cal = Calendar.getInstance();
    cal.setTime(getBeginDayOfWeek());
    cal.add(Calendar.DAY_OF_WEEK, 6);
    Date weekEndSta = cal.getTime();
    return getDayEndTime(weekEndSta);
  }

  public static DateRange getLastWeekDateRange(long time) {
    Calendar sundayCal = Calendar.getInstance();
    Calendar mondayCal = Calendar.getInstance();
    sundayCal.setTimeInMillis(time);
    mondayCal.setTimeInMillis(time);

    sundayCal.setFirstDayOfWeek(Calendar.MONDAY);
    mondayCal.setFirstDayOfWeek(Calendar.MONDAY);

    mondayCal.add(Calendar.DATE, -1 * 7);
    sundayCal.add(Calendar.DATE, -1 * 7);
    mondayCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    mondayCal.set(Calendar.HOUR_OF_DAY, 0);
    mondayCal.set(Calendar.MINUTE, 0);
    mondayCal.set(Calendar.SECOND, 0);
    sundayCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    sundayCal.set(Calendar.HOUR_OF_DAY, 23);
    sundayCal.set(Calendar.MINUTE, 59);
    sundayCal.set(Calendar.SECOND, 59);
    return new DateRange(mondayCal.getTimeInMillis(), sundayCal.getTimeInMillis());
  }

  public static DateRange getNextWeekDateRange() {
    Calendar sundayCal = Calendar.getInstance();
    Calendar mondayCal = Calendar.getInstance();
    sundayCal.setFirstDayOfWeek(Calendar.MONDAY);
    mondayCal.setFirstDayOfWeek(Calendar.MONDAY);

    mondayCal.add(Calendar.DATE, 1 * 7);
    sundayCal.add(Calendar.DATE, 1 * 7);
    mondayCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    mondayCal.set(Calendar.HOUR_OF_DAY, 0);
    mondayCal.set(Calendar.MINUTE, 0);
    mondayCal.set(Calendar.SECOND, 0);
    sundayCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    sundayCal.set(Calendar.HOUR_OF_DAY, 23);
    sundayCal.set(Calendar.MINUTE, 59);
    sundayCal.set(Calendar.SECOND, 59);
    return new DateRange(mondayCal.getTimeInMillis(), sundayCal.getTimeInMillis());
  }

  //获取本月的开始时间
  public static Date getBeginDayOfMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(getNowYear(), getNowMonth() - 1, 1);
    return getDayStartTime(calendar.getTime());
  }

  //获取月的开始时间
  public static long getBeginTimeByMonth(int monthIndex) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, getNowMonth() - 1 + monthIndex);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
  }

  //获取月的开始时间
  public static long getBeginTimeOfMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    calendar.set(Calendar.MONTH, getMonth(time) - 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
  }

  //获取月的开始时间
  public static long getBeginTimeOfLastMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    calendar.set(Calendar.MONTH, getMonth(time) - 2);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
  }

  public static long getEndTimeByMonth(int monthIndex) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, getNowMonth() - 1 + monthIndex);
//        calendar.set(Calendar.DAY_OF_MONTH, 1);
    @SuppressLint("WrongConstant") int day = calendar.getActualMaximum(5);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
  }

  public static long getEndTimeOfLastMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    calendar.set(Calendar.MONTH, getMonth(time) - 2);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.setTimeInMillis(calendar.getTimeInMillis());
    @SuppressLint("WrongConstant")
    int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
  }

  //获取本月的结束时间
  public static Date getEndDayOfMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(getNowYear(), getNowMonth() - 1, 1);
    @SuppressLint("WrongConstant") int day = calendar.getActualMaximum(5);
    calendar.set(getNowYear(), getNowMonth() - 1, day);
    return getDayEndTime(calendar.getTime());
  }

  //获取本年的开始时间
  public static java.util.Date getBeginDayOfYear() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getNowYear());
    // cal.set
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DATE, 1);

    return getDayStartTime(cal.getTime());
  }

  //获取本年的开始时间
  public static java.util.Date getBeginTimeOfYear(long time) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getYear(time));
    // cal.set
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DATE, 1);

    return getDayStartTime(cal.getTime());
  }

  //获取本年的开始时间
  public static java.util.Date getBeginTimeOfLastYear(long time) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getYear(time) - 1);
    // cal.set
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DATE, 1);

    return getDayStartTime(cal.getTime());
  }

  //获取本年的开始时间
  public static java.util.Date getEndTimeOfYear(long time) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getYear(time));
    // cal.set
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DATE, 31);

    return getDayEndTime(cal.getTime());
  }

  //获取本年的开始时间
  public static java.util.Date getEndTimeOfLastYear(long time) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getYear(time) - 1);
    // cal.set
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DATE, 31);

    return getDayEndTime(cal.getTime());
  }

  //获取本年的结束时间
  public static java.util.Date getEndDayOfYear() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, getNowYear());
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DATE, 31);
    return getDayEndTime(cal.getTime());
  }

  //获取某个日期的开始时间
  public static Timestamp getDayStartTime(Date d) {
    Calendar calendar = Calendar.getInstance();
    if (null != d) {
      calendar.setTime(d);
    }
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return new Timestamp(calendar.getTimeInMillis());
  }

  //获取某个日期的结束时间
  public static Timestamp getDayEndTime(Date d) {
    Calendar calendar = Calendar.getInstance();
    if (null != d) {
      calendar.setTime(d);
    }
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return new Timestamp(calendar.getTimeInMillis());
  }

  //获取某个日期的结束时间
  public static Timestamp getDayEndTime(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return new Timestamp(calendar.getTimeInMillis());
  }

  //获取今年是哪一年
  @SuppressLint("WrongConstant")
  public static Integer getNowYear() {
    Date date = new Date();
    GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
    gc.setTime(date);
    return Integer.valueOf(gc.get(1));
  }

  //获取本月是哪一月
  @SuppressLint("WrongConstant")
  public static int getNowMonth() {
    Date date = new Date();
    GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
    gc.setTime(date);
    return gc.get(2) + 1;
  }

  //两个日期相减得到的天数
  public static int getDiffDays(Date beginDate, Date endDate) {

    if (beginDate == null || endDate == null) {
      throw new IllegalArgumentException("getDiffDays param is null!");
    }

    long diff = (endDate.getTime() - beginDate.getTime())
        / (1000 * 60 * 60 * 24);

    int days = new Long(diff).intValue();

    return days;
  }

  //两个日期相减得到的毫秒数
  public static long dateDiff(Date beginDate, Date endDate) {
    long date1ms = beginDate.getTime();
    long date2ms = endDate.getTime();
    return date2ms - date1ms;
  }

  //获取两个日期中的最大日期
  public static Date max(Date beginDate, Date endDate) {
    if (beginDate == null) {
      return endDate;
    }
    if (endDate == null) {
      return beginDate;
    }
    if (beginDate.after(endDate)) {
      return beginDate;
    }
    return endDate;
  }

  //获取两个日期中的最小日期
  public static Date min(Date beginDate, Date endDate) {
    if (beginDate == null) {
      return endDate;
    }
    if (endDate == null) {
      return beginDate;
    }
    if (beginDate.after(endDate)) {
      return endDate;
    }
    return beginDate;
  }

  //返回某月该季度的第一个月
  public static Date getFirstSeasonDate(Date date) {
    final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int sean = SEASON[cal.get(Calendar.MONTH)];
    cal.set(Calendar.MONTH, sean * 3 - 3);
    return cal.getTime();
  }

  //返回某个日期下几天的日期
  public static Date getNextDay(Date date, int i) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
    return cal.getTime();
  }

  //返回某个日期前几天的日期
  public static Date getFrontDay(Date date, int i) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
    return cal.getTime();
  }

  //获取某年某月到某年某月按天的切片日期集合（间隔天数的日期集合）
  public static List getTimeList(int beginYear, int beginMonth, int endYear,
      int endMonth, int k) {
    List list = new ArrayList();
    if (beginYear == endYear) {
      for (int j = beginMonth; j <= endMonth; j++) {
        list.add(getTimeList(beginYear, j, k));

      }
    } else {
      {
        for (int j = beginMonth; j < 12; j++) {
          list.add(getTimeList(beginYear, j, k));
        }

        for (int i = beginYear + 1; i < endYear; i++) {
          for (int j = 0; j < 12; j++) {
            list.add(getTimeList(i, j, k));
          }
        }
        for (int j = 0; j <= endMonth; j++) {
          list.add(getTimeList(endYear, j, k));
        }
      }
    }
    return list;
  }

  //获取某年某月按天切片日期集合（某个月间隔多少天的日期集合）
  public static List getTimeList(int beginYear, int beginMonth, int k) {
    List list = new ArrayList();
    Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
    int max = begincal.getActualMaximum(Calendar.DATE);
    for (int i = 1; i < max; i = i + k) {
      list.add(begincal.getTime());
      begincal.add(Calendar.DATE, k);
    }
    begincal = new GregorianCalendar(beginYear, beginMonth, max);
    list.add(begincal.getTime());
    return list;
  }

  //获取某年某月的第一天日期
  public static Date getStartMonthDate(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);
    return calendar.getTime();
  }

  //获取某年某月的最后一天日期
  public static Date getEndMonthDate(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);
    @SuppressLint("WrongConstant") int day = calendar.getActualMaximum(5);
    calendar.set(year, month - 1, day);
    return calendar.getTime();
  }

  /**
   * 获取date的月份的时间范围
   */
  public static DateRange getMonthRange(Date date) {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(date);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMaxTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(date);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }


  /**
   * 获取昨天的时间范围
   */
  public static DateRange getYesterdayRange() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.add(Calendar.DAY_OF_MONTH, -1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.add(Calendar.DAY_OF_MONTH, -1);
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取当前月份的时间范围
   */
  public static DateRange getThisMonth() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取上个月的时间范围
   */
  public static DateRange getLastMonth() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.add(Calendar.MONTH, -1);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.add(Calendar.MONTH, -1);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取上个季度的时间范围
   */
  public static DateRange getLastQuarter() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取上个季度的时间范围
   */
  public static DateRange getLastQuarter(long time) {

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTimeInMillis(time);
    startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTimeInMillis(time);
    endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取当前季度的时间范围
   *
   * @return current quarter
   */
  public static DateRange getThisQuarter() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  /**
   * 获取下个季度的时间范围
   *
   * @return current quarter
   */
  public static DateRange getNextQuarter(long time) {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTimeInMillis(time);
    startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 + 1) * 3);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    setMinTime(startCalendar);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTimeInMillis(time);
    endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
    endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    setMaxTime(endCalendar);

    return new DateRange(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
  }

  private static void setMinTime(Calendar calendar) {
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
  }

  private static void setMaxTime(Calendar calendar) {
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
    calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
    calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
  }


  public final static String DEFAULT_PATTERN = "MM/dd/yyyy HH:mm:ss";

  public static String format(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
    return sdf.format(date);
  }

  //    Calendar c = Calendar.getInstance();
//
//    //过去七天
//        c.setTime(new Date());
//        c.add(Calendar.DATE, - 7);
//    Date d = c.getTime();
//    String day = format.format(d);
//        System.out.println("过去七天："+day);
//
//    //过去一月
//        c.setTime(new Date());
//        c.add(Calendar.MONTH, -1);
//    Date m = c.getTime();
//    String mon = format.format(m);
//        System.out.println("过去一个月："+mon);
//
//    //过去三个月
//        c.setTime(new Date());
//        c.add(Calendar.MONTH, -3);
//    Date m3 = c.getTime();
//    String mon3 = format.format(m3);
//        System.out.println("过去三个月："+mon3);
//
//    //过去一年
//        c.setTime(new Date());
//        c.add(Calendar.YEAR, -1);
//    Date y = c.getTime();
//    String year = format.format(y);
  public static String getOpenBallDay(Long startTime) {
    int day = getDayString(startTime);
    String str = "可预订时间";
    switch (day) {
      case 0:
        str = "今日可预订时间";
        break;
      case 1:
        str = "明日可预订时间";
        break;
      case 2:
        str = "可预订时间";
        break;
      default:
        break;
    }
    return str;
  }

  public static int getDayNum(long s) {
    return s > 0 ? (int) (s / (24 * 60 * 60 * 1000)) : 0;
  }

  public static int getHourNum(long s) {
    return s > 0 ? (int) ((s % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000)) : 0;
  }

  public static String getHourSecond(long s) {
    return new StringBuilder().append(getHour(s))
        .append("小时")
        .append(getSecond(s))
        .append("分")
        .toString();
  }

  public static int getSecond(long s) {
    return s > 0 ? (int) ((s / 60 / 1000) % 60) : 0;
  }

  public static int getHour(long s) {
    return s > 0 ? (int) (s / (60 * 60 * 1000)) : 0;
  }

  public static int getMinuteNum(long s) {
    return s > 0 ? (int) ((s % (60 * 60 * 1000)) / (60 * 1000)) : 0;
  }

  public static int getSecondNum(long s) {
    return s > 0 ? (int) (s / 1000 % 60) : 0;
  }

  public static long beforeTomorrow() {
    Log.e("today-end-----------", stampToDate(getDayEnd().getTime() + ""));
    return getDayEnd().getTime() - System.currentTimeMillis();
  }

  public static long getOneDay() {
    return 24 * 60 * 60 * 1000;
  }


  /**
   * 判断时间是不是今天
   *
   * @return 是返回true，不是返回false
   */
  public static boolean isToday(long time) {
    try {
      //当前时间
      Date now = new Date();
      SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
      //获取今天的日期
      String nowDay = sf.format(now);
      //对比的时间
      String day = sf.format(new Date(time));
      return day.equals(nowDay);
    } catch (Exception e) {
      return false;
    }
  }

  public static long getTimeByYMD(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, day);
    return calendar.getTime().getTime();
  }
}
